package com.sukaiyi.giteeideaplugin.cellrenderer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBLoadingPanel;
import com.intellij.util.ui.JBFont;
import com.sukaiyi.giteeideaplugin.utils.HumanizedDateUtils;
import icons.PluginIcons;

import javax.swing.*;
import java.awt.*;
import java.util.Optional;

/**
 * @author sukaiyi
 * @date 2020/07/01
 */
public class RepoListCell {
    private JBLabel iconLabel;
    private JBLabel nameLabel;
    private JPanel content;
    private JBLabel starIconLabel;
    private JBLabel watchIconLabel;
    private JBLabel forkIconLabel;
    private JBLabel descriptionLabel;
    private JBLabel languageLabel;
    private JBLabel pushedAtLabel;
    private final JSONObject repo;

    public RepoListCell(JSONObject repo) {
        this.repo = repo;
        nameLabel.setText(repo.getStr("human_name"));
        boolean isPrivate = repo.getBool("private", false);
        boolean isPublic = repo.getBool("public", false);
        boolean isInternal = repo.getBool("internal", false);
        boolean isFork = repo.getBool("fork", false);
        boolean stared = repo.getBool("stared", false);
        boolean watched = repo.getBool("watched", false);
        int stargazersCount = repo.getInt("stargazers_count", 0);
        int watchersCount = repo.getInt("watchers_count", 0);
        int forksCount = repo.getInt("forks_count", 0);
        String pushed_at = repo.getStr("pushed_at", "2020-06-22T11:47:59+08:00");

        iconLabel.setIcon(isPrivate ? PluginIcons.LOCK_DARK : PluginIcons.REPO_DARK);
        starIconLabel.setText(String.valueOf(stargazersCount));
        starIconLabel.setIcon(stared ? PluginIcons.STAR : IconLoader.getDisabledIcon(PluginIcons.STAR));
        watchIconLabel.setText(String.valueOf(watchersCount));
        watchIconLabel.setIcon(watched ? PluginIcons.WATCH : IconLoader.getDisabledIcon(PluginIcons.WATCH));
        forkIconLabel.setText(String.valueOf(forksCount));
        forkIconLabel.setIcon(PluginIcons.FORK);
        descriptionLabel.setText(StrUtil.maxLength(repo.getStr("description"), 30));
        languageLabel.setText(repo.getStr("language"));
        pushedAtLabel.setText(
                Optional.of(pushed_at)
                        .map(at -> DateUtil.parse(at, "yyyy-MM-dd'T'HH:mm:ss'+08:00'"))
                        .map(HumanizedDateUtils::humanize)
                        .orElse("")
        );
    }

    public JPanel getContent() {
        return content;
    }

    public boolean isEnabled() {
        return content.isEnabled();
    }

    public void setForeground(Color color) {
        content.setForeground(color);
        nameLabel.setForeground(color);
        starIconLabel.setForeground(color);
        watchIconLabel.setForeground(color);
        forkIconLabel.setForeground(color);

        int red = color.getRed();
        int green = color.getGreen();
        int blue = color.getBlue();
        boolean isPrivate = repo.getBool("private", false);
        if ((red + green + blue) / 3 < 128) {
            iconLabel.setIcon(isPrivate ? PluginIcons.LOCK_DARK : PluginIcons.REPO_DARK);
        } else {
            iconLabel.setIcon(isPrivate ? PluginIcons.LOCK_LIGHT : PluginIcons.REPO_LIGHT);
        }
    }

    public void setBackground(Color color) {
        content.setBackground(color);
    }
}
